/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PrerequesiteViewer2.java
 *
 * Created on 28.08.2009, 11:05:06
 */
package at.tugraz.kmi.css.cbkst2.prereq.structurevis;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.ServiceLoader;

import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import at.tugraz.kmi.css.cbkst2.CbKSTCompetence;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.jgoodies.binding.adapter.BasicComponentFactory;

/**
 * Enclosing Panel for the {@link StructureClosureView} and {@link HasseView}
 * Visualisation.
 * 
 * @author Georg Öttl <georg.oettl@gmail.com>
 */
public final class CompetenceManagerView extends JPanel {

	private static final long				serialVersionUID	= 1L;
	private final HasseView					hasseViewer;
	private final StructureClosureView		closureViewer;
	private final CompetenceManagerModel	competenceManagerModel;

	public static void main(String[] args) {
		Provider<CompetenceManagerView> provider = Guice.createInjector(
				ServiceLoader.load(com.google.inject.Module.class))
				.getProvider(CompetenceManagerView.class);
		CompetenceManagerView viewer = provider.get();
		JFrame frame = new JFrame();
		frame.getContentPane().add(viewer);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * Get the Strings to filter against for a given Issue.
	 * 
	 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
	 */
	public static class IssueTextFilterator implements
			TextFilterator<CbKSTCompetence> {

		@Override
		public void getFilterStrings(List<String> baseList,
				CbKSTCompetence issue) {
			baseList.add(issue.getName());
			baseList.add(issue.getDescription());
		}
	}

	private final CompetenceEditorView					competencePanel;
	private TextComponentMatcherEditor<CbKSTCompetence>	editor;

	@Inject
	CompetenceManagerView(CompetenceManagerModel model,
			final HasseView hasseViewer,
			final StructureClosureView closureViewer,
			final CompetenceEditorView competencePanel) {
		competenceManagerModel = model;
		this.hasseViewer = hasseViewer;
		this.closureViewer = closureViewer;
		this.competencePanel = competencePanel;
		initComponents();
		connectComponents();
	}

	private void connectComponents() {

		competenceManagerModel.addPropertyChangeListener(
				CompetenceManagerModel.PROPERTY_SHOW_TRANSITIVE_CLOSURE_VIEW,
				new PropertyChangeListener() {

					@Override
					public void propertyChange(PropertyChangeEvent evt) {
						boolean isEnabled = (Boolean) evt.getNewValue();
						if (!isEnabled) {
							jTabbedPane1.remove(jPanelClosureViewer);
						} else {
							jTabbedPane1.add(jPanelClosureViewer);
						}
					}
				});
		jPanelItemDetail.add(competencePanel);
		jPanelItemDetail.revalidate();
		editor = new TextComponentMatcherEditor<CbKSTCompetence>(
				jTextFieldFilterEdit, new IssueTextFilterator());
		FilterList<CbKSTCompetence> competencesEventList2 = competenceManagerModel
				.getCompetencesEventList();
		competencesEventList2.setMatcherEditor(editor);

		jTextFieldFilterEdit.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
			}

			@Override
			public void focusGained(FocusEvent e) {
				jList1.clearSelection();
			}
		});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jTabbedPane1 = new javax.swing.JTabbedPane();
		jPanelPrerequesites = hasseViewer;
		jPanelClosureViewer = closureViewer;
		jPanelTabularView = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jPanelDetails = new javax.swing.JPanel();
		jPanelFilterCompetences = new javax.swing.JPanel();
		jTextFieldFilterEdit = new javax.swing.JTextField();
		jScrollPaneItems = new javax.swing.JScrollPane();
		jList1 = BasicComponentFactory.createList(competenceManagerModel
				.getCompetenceSelection());
		jPanelItemDetail = new javax.swing.JPanel();

		setMaximumSize(new java.awt.Dimension(720, 630));
		setMinimumSize(new java.awt.Dimension(720, 570));
		setName("CompetenceStructureEditor"); // NOI18N
		setPreferredSize(new java.awt.Dimension(720, 630));
		setLayout(new java.awt.BorderLayout());

		jTabbedPane1.setMaximumSize(new java.awt.Dimension(800, 630));
		jTabbedPane1.setMinimumSize(new java.awt.Dimension(800, 570));
		jTabbedPane1.setName("PrerequesiteViewer"); // NOI18N
		jTabbedPane1.setPreferredSize(new java.awt.Dimension(800, 570));

		jPanelPrerequesites.setName("tabPrerequesiteEditor"); // NOI18N
		jPanelPrerequesites.setLayout(new java.awt.BorderLayout());
		jTabbedPane1.addTab("Prerequesite Editor", jPanelPrerequesites);

		jPanelClosureViewer.setBackground(java.awt.Color.white);
		jPanelClosureViewer.setName("tabClosureViewer"); // NOI18N

		javax.swing.GroupLayout jPanelClosureViewerLayout = new javax.swing.GroupLayout(
				jPanelClosureViewer);
		jPanelClosureViewer.setLayout(jPanelClosureViewerLayout);
		jPanelClosureViewerLayout.setHorizontalGroup(jPanelClosureViewerLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 436, Short.MAX_VALUE));
		jPanelClosureViewerLayout.setVerticalGroup(jPanelClosureViewerLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 599, Short.MAX_VALUE));

		jTabbedPane1.addTab("Closure View (RO)", jPanelClosureViewer);

		jPanelTabularView.setLayout(new java.awt.BorderLayout());

		jTable1.setModel(competenceManagerModel.getCompetenceTableModel());
		jTable1.setCellEditor(new DefaultCellEditor(new JTextField()));
		jScrollPane1.setViewportView(jTable1);

		jPanelTabularView.add(jScrollPane1, java.awt.BorderLayout.CENTER);

		jTabbedPane1.addTab("Tabular View (RO)", jPanelTabularView);

		add(jTabbedPane1, java.awt.BorderLayout.CENTER);

		jPanelDetails.setMaximumSize(new java.awt.Dimension(300, 630));
		jPanelDetails.setMinimumSize(new java.awt.Dimension(300, 570));
		jPanelDetails.setLayout(new java.awt.BorderLayout());

		jPanelFilterCompetences.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Filter Competences"));
		jPanelFilterCompetences
				.setPreferredSize(new java.awt.Dimension(280, 70));

		jTextFieldFilterEdit.setPreferredSize(new java.awt.Dimension(200, 27));
		jTextFieldFilterEdit
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jTextFieldFilterEditActionPerformed(evt);
					}
				});
		jPanelFilterCompetences.add(jTextFieldFilterEdit);

		jPanelDetails.add(jPanelFilterCompetences, java.awt.BorderLayout.NORTH);

		jScrollPaneItems.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Competence List"));
		jScrollPaneItems.setAutoscrolls(true);
		jScrollPaneItems.setMaximumSize(new java.awt.Dimension(280, 330));
		jScrollPaneItems.setMinimumSize(new java.awt.Dimension(280, 330));
		jScrollPaneItems.setPreferredSize(new java.awt.Dimension(200, 330));
		jScrollPaneItems.setVerifyInputWhenFocusTarget(false);

		jList1.setCellRenderer(new CompetenceItem());
		jScrollPaneItems.setViewportView(jList1);

		jPanelDetails.add(jScrollPaneItems, java.awt.BorderLayout.CENTER);

		jPanelItemDetail.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Details"));
		jPanelItemDetail.setMaximumSize(new java.awt.Dimension(280, 300));
		jPanelItemDetail.setMinimumSize(new java.awt.Dimension(280, 240));
		jPanelItemDetail.setPreferredSize(new java.awt.Dimension(280, 300));
		jPanelItemDetail.setLayout(new java.awt.BorderLayout());
		jPanelDetails.add(jPanelItemDetail, java.awt.BorderLayout.SOUTH);

		add(jPanelDetails, java.awt.BorderLayout.EAST);
	}// </editor-fold>//GEN-END:initComponents

	private void jTextFieldFilterEditActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jTextFieldFilterEditActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jTextFieldFilterEditActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton1ActionPerformed
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private javax.swing.JList		jList1;
	private javax.swing.JPanel		jPanelClosureViewer;
	private javax.swing.JPanel		jPanelDetails;
	private javax.swing.JPanel		jPanelFilterCompetences;
	private javax.swing.JPanel		jPanelItemDetail;
	private javax.swing.JPanel		jPanelPrerequesites;
	private javax.swing.JPanel		jPanelTabularView;
	private javax.swing.JScrollPane	jScrollPane1;
	private javax.swing.JScrollPane	jScrollPaneItems;
	private javax.swing.JTabbedPane	jTabbedPane1;
	private javax.swing.JTable		jTable1;
	private javax.swing.JTextField	jTextFieldFilterEdit;
	// End of variables declaration//GEN-END:variables
}
